package com.zjzywl.smoke.controller;

import java.util.LinkedHashMap;

import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.zjzywl.smoke.util.Items;
import com.zjzywl.smoke.util.WebResult;

import net.sf.json.JSONObject;

@RestController
public class LightController extends BaseController {
	@RequestMapping(value = "/light/contorller/{devEUI}/{status}")
	public WebResult oneWay(@PathVariable("devEUI") String devEUI, @PathVariable("status") int status) {
		System.out.println("测试url：" + appConfig.getLightserviceip() + appConfig.getLightserviceport()
				+ "/light/contorller/" + devEUI + "/" + status);
		// 单路灯控接口
		appConfig.restTemplate().exchange(appConfig.getLightserviceip() + appConfig.getLightserviceport()
				+ "/light/contorller/" + devEUI + "/" + status, HttpMethod.GET, null, String.class);
		System.out.println("测试url：" + appConfig.getLightserviceip() + appConfig.getLightserviceport()
				+ "/light/contorller/" + devEUI + "/" + status);
		return WebResult.OK();
	}

	@RequestMapping(value = "/lights/contorller", method = RequestMethod.POST)
	public WebResult moreWay(@RequestBody Items items) {
		// 多路灯控接口
		String url = appConfig.getLightserviceip() + appConfig.getLightserviceport() + "/lights/contorller";
		System.out.println("测试url" + url);
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		Map<String, String> hashMap = new LinkedHashMap<String, String>();
		hashMap.put("cmd", items.getCmd());
		hashMap.put("devCodes", items.getDevCodes());
		JSONObject jsonObj = JSONObject.fromObject(hashMap);
		HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
		String result = restTemplate.postForObject(url, formEntity, String.class);
		System.out.println("测试url" + url);
		return WebResult.OK();
	}

}
